package com.oa.pm.controller;

import com.joneying.common.web.response.Resp;
import com.oa.core.PageResult;
import com.oa.core.exception.BusinessException;
import com.oa.core.exception.DataIsEmptyException;
import com.oa.pm.dto.SkillUpgradTrainingDTO;
import com.oa.pm.dto.SkillUpgradTrainingParamDTO;
import com.oa.pm.entity.SkillUpgradTraining;
import com.oa.pm.service.ISkillUpgradTrainingService;
import com.oa.pm.vo.SkillUpgradTrainingDetailsVO;
import com.oa.pm.vo.SkillUpgradTrainingListVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.joneying.common.annotation.CommonController;
import org.springframework.context.annotation.Description;

import java.util.List;

import org.springframework.web.bind.annotation.RestController;
import com.oa.core.BaseController;

/**
 * @author syb123
 * @since 2019-01-22
 */
@RestController
@RequestMapping("/skillUpgradTraining")
@Api(description = "技能提升培训表模块")
public class SkillUpgradTrainingController extends BaseController {

    private final ISkillUpgradTrainingService skillUpgradTrainingService;

    @Autowired
    public SkillUpgradTrainingController(ISkillUpgradTrainingService skillUpgradTrainingService) {
        this.skillUpgradTrainingService = skillUpgradTrainingService;
    }

    @GetMapping("/list")
    @CommonController
    @Description("查询所有")
    @ApiOperation(value = "查询")
    public Resp<PageResult<SkillUpgradTrainingListVO>> findSkillUpgradTrainingList(@ApiParam(value = "培训查询条件对象") SkillUpgradTrainingParamDTO skillUpgradTrainingParamDTO) {
        skillUpgradTrainingParamDTO.setUserId(getLoginUserId());
        return new Resp().success(skillUpgradTrainingService.findSkillUpgradTrainingList(skillUpgradTrainingParamDTO));
    }

    @GetMapping("/export")
    @CommonController
    @Description("查询所有")
    @ApiOperation(value = "查询")
    public void export(@ApiParam(value = "培训查询条件对象") SkillUpgradTrainingParamDTO skillUpgradTrainingParamDTO) {
        skillUpgradTrainingParamDTO.setUserId(getLoginUserId());
        skillUpgradTrainingService.export(skillUpgradTrainingParamDTO, response);
    }

    /**
     * 根据Id查询
     *
     * @param id id
     * @return Resp
     */
    @GetMapping("/{id}")
    @CommonController
    @Description("获取详细信息")
    @ApiOperation(value = "根据id查询")
    public Resp<SkillUpgradTrainingDetailsVO> findById(@ApiParam(value = "id", required = true) @PathVariable String id) {
        return new Resp<SkillUpgradTrainingDetailsVO>().success(skillUpgradTrainingService.findSkillUpgradTrainingById(id));
    }


    /**
     * 新增
     *
     * @param skillUpgradTraining SkillUpgradTraining
     * @return Resp
     */
    @PostMapping("")
    @CommonController
    @Description("新增")
    @ApiOperation(value = "新增")
    public Resp add(@ApiParam(value = "技能提升培训表对象") @RequestBody(required = false) SkillUpgradTrainingDTO skillUpgradTraining) throws BusinessException, DataIsEmptyException {
        skillUpgradTraining.setUserId(getLoginUserId());
        return skillUpgradTrainingService.addSkillUpgradTraining(skillUpgradTraining);
    }

    /**
     * 根据Id修改
     *
     * @param
     * @return Resp
     */
    @PutMapping("")
    @CommonController
    @Description("根据id修改")
    @ApiOperation(value = "根据id修改")
    public Resp update(@ApiParam(value = "技能提升培训表对象") @RequestBody(required = false) SkillUpgradTrainingDTO skillUpgradTrainingDTO) {
        SkillUpgradTraining skillUpgradTraining = new SkillUpgradTraining();
        BeanUtils.copyProperties(skillUpgradTrainingDTO, skillUpgradTraining);
        if (skillUpgradTrainingService.updateById(skillUpgradTraining)) {
            return new Resp().success();
        } else {
            return new Resp().failure();
        }
    }

    /**
     * 根据Id删除
     *
     * @param id id
     * @return Resp
     */
    @DeleteMapping("/{id}")
    @CommonController
    @Description("根据id删除")
    @ApiOperation(value = "根据id删除")
    public Resp delete(@ApiParam(value = "id", required = true) @PathVariable("id") String id) {
        SkillUpgradTraining skillUpgradTraining = skillUpgradTrainingService.selectById(id);
        if (skillUpgradTraining != null && skillUpgradTraining.getIsDeleted() == 2) {
            skillUpgradTraining.setIsDeleted(1);
            if (skillUpgradTrainingService.updateById(skillUpgradTraining)) {
                return new Resp().success();
            }
            return new Resp().failure();
        }
        return new Resp().failure();
    }
}
